from sklearn.datasets import load_wine
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import precision_score, recall_score
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import numpy as np

wine = load_wine()
X = wine.data
y = wine.target
scaler = MinMaxScaler()
scaler.fit(X)
X = scaler.transform(X)

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                       random_state = 60)

score = [[],[]]
precision_train = [[],[]]; recall_train = [[],[]]
precision_test = [[],[]]; recall_test = [[],[]]

classification = [0,0]
x = [0.1,1,5,10]
LOGISTIC_REGRESSION = 0; SVM = 1

for hyper_value in x:  
    for i in (LOGISTIC_REGRESSION, SVM):
        if (i == LOGISTIC_REGRESSION):
            classification[LOGISTIC_REGRESSION] =
            LogisticRegression(C=hyper_value, solver = 'newton-cg', 
                               multi_class = 'multinomial')
        else:
            classification[SVM] = svm.SVC(kernel='rbf', gamma=hyper_value,
                                          C=1)      
        classification[i].fit(X_train,y_train)  
        score[i].append(np.mean(cross_val_score(classification[i], X, y,
                                cv=3)))
        y_pred_train = classification[i].predict(X_train)
        y_pred_test = classification[i].predict(X_test)
        precision_train[i].append(precision_score(y_train, y_pred_train,
                                                  average = 'weighted'))
        recall_train[i].append(recall_score(y_train, y_pred_train, 
                                            average = 'weighted'))
        precision_test[i].append(precision_score(y_test, y_pred_test,
                                                 average = 'weighted'))
        recall_test[i].append(recall_score(y_test, y_pred_test, 
                                           average = 'weighted'))
    
fig, axs = plt.subplots(1,2,figsize=(17,4))
for i in (LOGISTIC_REGRESSION, SVM):
    axs[i].plot(x,recall_train[i], color='green', linestyle=':',
                linewidth=4.0, label='Train recall')
    axs[i].plot(x,precision_train[i], color='blue', linestyle=':',
                linewidth=4.0, label='Train precision')
    axs[i].plot(x,recall_test[i], color='orange', linestyle=':',
                linewidth=4.0, label='Test recall')
    axs[i].plot(x,precision_test[i], color='cyan', linestyle=':',
                linewidth=4.0, label='Test precision')
    axs[i].plot(x,score[i], color='red', linestyle=':', linewidth=4.0,
                label='Score')
    if (i == LOGISTIC_REGRESSION):
        axs[i].set_title('Logistic Regression quality values')
    else:
        axs[i].set_title('SVM quality values') 
    axs[i].grid();
    axs[i].legend(fontsize = '10')
plt.show()  
